package com.gj.baselib.sysutils;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.telephony.TelephonyManager;

public class AndroidSysUtils {
	
	/**
	 * SIM卡状态
	 */
	public final static int SIM_STATE_UNKNOWN = 0; /* 未知 */
	public final static int SIM_STATE_ABSENT = 1; /* 离线 */
	public final static int SIM_STATE_PIN_REQUIRED = 2; /* 需要PIN */
	public final static int SIM_STATE_PUK_REQUIRED = 3; /* 需要PUK */
	public final static int SIM_STATE_NETWORK_LOCKED = 4; /* 网络锁定 */
	public final static int SIM_STATE_READY = 5; /* 可读 */

	private static Map<Integer, Integer> simStateMap = new HashMap<Integer, Integer>();
	
	/**
	 * 获得磁盘的缓存目录
	 * 
	 * @param context
	 *            上下文
	 * @return
	 */
	public static String getDiskCacheDirName(Context context) {
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable()) {
			return context.getExternalCacheDir().getPath();
		} else {
			return context.getCacheDir().getPath();
		}
	}

	/**
	 * @Title: Dip转PX
	 * @Description:通过DIP换算成PX
	 * @param context
	 *            上下文
	 * @param dpValue
	 *            dip值
	 * @return 换算后的px值
	 * @throws 无
	 * @date Dec 16, 2012 9:05:05 PM
	 */
	public static int dipToPx(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * @Title: PX转DIP值
	 * @Description: 通过PX换算成DIP
	 * @param @param context 上下文
	 * @param @param pxValue 像素值
	 * @return 换算后的dip值
	 * @throws 无
	 * @date Dec 16, 2012 9:06:00 PM
	 */
	public static int pxToDip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	static {
		simStateMap.put(TelephonyManager.SIM_STATE_UNKNOWN, SIM_STATE_UNKNOWN);
		simStateMap.put(TelephonyManager.SIM_STATE_ABSENT, SIM_STATE_ABSENT);
		simStateMap.put(TelephonyManager.SIM_STATE_PIN_REQUIRED,
				SIM_STATE_PIN_REQUIRED);
		simStateMap.put(TelephonyManager.SIM_STATE_PUK_REQUIRED,
				SIM_STATE_PUK_REQUIRED);
		simStateMap.put(TelephonyManager.SIM_STATE_NETWORK_LOCKED,
				SIM_STATE_NETWORK_LOCKED);
		simStateMap.put(TelephonyManager.SIM_STATE_READY, SIM_STATE_READY);
	}

	/**
	 * @Description: 返回手机型号
	 * @param 无
	 * @return 手机型号
	 * @throws 无
	 */
	public static String getModel() {
		return android.os.Build.MODEL;
	}

	/**
	 * @Description: 返回手机SDK版本
	 * @param 无
	 * @return 当前系统SDK版本号
	 * @throws 无
	 */
	public static int getSdk() {
		return android.os.Build.VERSION.SDK_INT;
	}

	/**
	 * @Description: 获得版本号
	 * @param 无
	 * @return 当前系统版本号
	 * @throws 无
	 */
	public static String getRelease() {
		return android.os.Build.VERSION.RELEASE;
	}

	/**
	 * @Description: 获得设备唯一标识
	 * @param activity
	 *            Activity 实例
	 * @return 设备标识
	 * @throws 无
	 */
	public static String getImei(Activity activity) {
		TelephonyManager telephonyManager = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);

		return telephonyManager.getDeviceId();
	}

	/**
	 * @Description: 获得设备手机号
	 * @param activity
	 *            Activity 实例
	 * @return 手机号码
	 * @throws 无
	 */
	public static String getPhoneNumber(Activity activity) {
		TelephonyManager telephonyManager = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);

		return telephonyManager.getLine1Number();
	}

	/**
	 * @Description: 获得设备IMSI信息
	 * @param activity
	 *            Activity 实例
	 * @return IMSI信息
	 * @throws 无
	 */
	public static String getImsi(Activity activity) {
		TelephonyManager telephonyManager = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);

		return telephonyManager.getSubscriberId();
	}

	/**
	 * @Description: 返回网络运营商名称,getSimState必须是SIM_STATE_READY
	 * @param activity
	 *            Activity 实例
	 * @return 网络运营商名称
	 * @throws 无
	 */
	public static String getSimOperatorName(Activity activity) {
		String simOperator = getSimOperator(activity);

		// 中国移动
		if (simOperator.equals("46000") || simOperator.equals("46002")
				|| simOperator.equals("46007")) {
			return "移动";
			// 中国联通
		} else if (simOperator.equals("46001")) {
			return "联通";
			// 中国电信
		} else {
			return "电信";
		}
	}

	/**
	 * @Description: 返获得运营商编码
	 * @param activity
	 *            Activity实例
	 * @return 运营商编码
	 * @throws
	 * @date Dec 12, 2012 11:41:21 AM
	 */
	public static String getSimOperator(Activity activity) {
		TelephonyManager telephonyManager = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);

		return telephonyManager.getSimOperator();
	}

	/**
	 * @Description:获得SIM卡状态
	 * @param activity
	 *            Activity 实例
	 * @return SIM_STATE_UNKNOWN:未知 SIM_STATE_ABSENT:离线
	 *         SIM_STATE_PIN_REQUIRED:需要PIN码 SIM_STATE_PUK_REQUIRED:需要PUK
	 *         SIM_STATE_NETWORK_LOCKED:网络锁定 SIM_STATE_READY:可读
	 * @throws 无
	 */
	public static int getSimState(Activity activity) {
		TelephonyManager telephonyManager = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);

		return simStateMap.get(telephonyManager.getSimState());
	}
}
